
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

#include "concurrent_queue.h"

void *producerThread(void *value) {
    struct conQueue *queue = (struct conQueue *)value;
    printf("producerThread threadID %lu\n", pthread_self());
    for (int i = 0; i < 10; ++i)
        cqEnqueue(queue, i);
    return NULL;
}

void *consumerThread(void *value) {
    struct conQueue *queue = (struct conQueue *)value;
    int val = 0;
    for (int i = 0; i < 10; ++i) {
        val = -1;
        cqDequeue(queue, &val);
        printf("consumer threadID %lu get: %d\n", pthread_self(), val);
    }
    return NULL;
}

int main(int argc, char **argv) {
    struct conQueue queue;
    int i = 0;

    cqInitQueue(&queue);
    while (i++ < 5) {
        pthread_t handler;
        pthread_create(&handler, NULL, producerThread, &queue);
    }
    while (i++ < 10) {
        pthread_t handler;
        pthread_create(&handler, NULL, consumerThread, &queue);
    }
    cqDestroyQueue(&queue);
    sleep(3);
    printf("end\n");
    return 0;
}